//给商品添加属性id
define(function(){
	function Index(){
		attrId();
		function attrId(){
		var count = $(".shop");
		var count2 = $(".buynow")
		var id = 0;
		count.each(function(){
			$(this).attr("com-id",id);
		id++
		})
		count2.each(function(){
			$(this).attr("com-id",id);
			id++
	})
}
//slider
scroll();

//三级菜单
threeNav()
function threeNav(){
	$(".nav_secondUl_li").hover(
		function(){
			$(this).find(".home_nav_three").stop().animate({
				right:-750
			},300);
			$(this).find(".home_nav_three").css("display","block")
		},
		function(){
			$(this).find(".home_nav_three").stop().animate({
				right:-730
			},300);
			$(this).find(".home_nav_three").css("display","none")
		}
	)
	
	}
//brand
	$(".brand_sale ul li").hover(
			function(){
				$(this).find(".act_name").stop().animate({
					top:0
				},500)
			},
			function(){
				$(this).find(".act_name").stop().animate({
					top:97
				},500)
			}
		)
//两侧的工具栏
function scroll(){
	document.onscroll = function(){
		var sTop = document.documentElement.scrollTop;	
		if(sTop >= 700){
			$("#toolbar2").css("display","block");
			$("#toolbar3").css("display","block");
		}else if(sTop<700){
			$("#toolbar2").css("display","none");
			$("#toolbar3").css("display","none");
		}
	}
}
//倒计时抢购
cutTime();
function cutTime(){
	var date = new Date();
	var now = date.getTime();
	var newDate = new Date("2018-1-27 21:40:00");
	var will = newDate.getTime();
	var dis = will - now;
	var d,h,m,s;
	var t
	if(dis>0){
		d = Math.floor(dis / 1000 / 60 / 60 / 24);
		h = Math.floor(dis / 1000 / 60 / 60 % 24);
		m = Math.floor(dis / 1000 / 60 % 60);
		s = Math.floor(dis / 1000 % 60);
	}else{
		clearInterval(t)
	}
	if(s<10&&s>=0){
		s = "0"+ s; 
	}
	if(d<10&&d>=0){
		d = "0"+ d; 
	}
	if(h<10&&h>=0){
		h = "0"+ h; 
	}
	if(m<10&&m>=0){
		m = "0"+ m; 
	}
	$(".day").html(d);
	$(".hour").html(h);
	$(".min").html(m);
	$(".secend").html(s);
	t = setInterval(function(){
		cutTime();
	},1000)
}
jump()
function jump(){
	$("#log a").click(function(){
		window.location = "log.html";
	})
	$("#login a").click(function(){
		window.location = "login.html"
	})
	$(".comImg,.comname").click(function(){
		window.location = "particulars.html";
		
	})
	$(".fistUl_li_a").click(function(){
		window.location = "index.html"
	})
	
}

$(".shop,.buynow").click(function(){
	window.location.reload();
	var img = $(this).parent().parent().find(".comImg").find("img").attr("src");
	var name = $(this).parent().parent().find(".comname").html();
	var price = $(this).parent().find(".price").html();
	var id = $(this).attr("com-id");
	var _key = "com_"+id;
	var _value = '{"name":"' + name + '","price":'+ price + ',"img":"'+ img +'","num":'+1 +',"id":'+id+'}';
	var cookstr = $.cookie(_key)?$.cookie(_key):"";
	if(cookstr){
		var num = JSON.parse(cookstr).num;
		num++
		$.cookie(_key,null,{expires:7,path:"/"});
		var _value = '{"name":"' + name + '","price":'+ price + ',"img":"'+ img +'","num":'+num+',"id":'+id+'}';
		$.cookie(_key,_value,{expires:7,path:"/"});
	}else{
		$.cookie(_key,_value,{expires:7,path:"/"});
	}
	
//	$(".buylist").append('<li><a class="buy_img" href="img"></a><p><a class="buy_name" href="">111111111111</a></p><p>￥<span class="buy_price">2*12</span>件</p></li>')
})
	addli();
	function addli(){
		var	cookarr = document.cookie.split("; ");
		var list = $(".comid");
		var count = 0;
		$(".fistUl_li_span").click(function(){
			window.location.href = "../shopcar.html"
		})
		cookarr.forEach(function(value,index){
			var keyvalue = value.split("=");
			if(keyvalue[0] !="user"&&keyvalue[0]!= ""){
			var _key = keyvalue[0];
			var value = $.cookie(_key);
			var cook = JSON.parse(value);
			count += cook.num;
			$(".fistUl_li_span").html(count);
		$(".buylist").append('<li><a class="buy_img"><img src="'+cook.img+'"/></a><p><a class="buy_name" href="">'+cook.name+'</a></p><p>￥<span class="buy_price">'+cook.price+'*'+cook.num+'</span>件</p><i class="comid">'+cook.id+'</i></li>');
		}
		})
	}
	
	
	addshop();
	function addshop(){
		var	cookarr = document.cookie.split("; ");
		var list = $(".comid");
		cookarr.forEach(function(value,index){
			var keyvalue = value.split("=");
			if(keyvalue[0] !=="user"&&keyvalue[0]!==""){
			var value = $.cookie(keyvalue[0]);
			var cook = JSON.parse(value);
			$(".shopcar_ul").append('<li ><img class="shopcar_com_img" src="'+cook.img +'" alt="" /><a class="shopcar_com_name" href="">'+ cook.name +'</a><b>'+ cook.price+'</b><i>'+ cook.price+'</i><h4><span class="jian">-</span><input class="shop_buynum" type="text" value="'+cook.num+'"/><span class="jia">+</span></h4><em>'+ cook.price+'</em><h3>'+ cook.price*cook.num+'</h3><h5 class = "shop_id">'+ cook.id +'</h5></li>');		
			}
		})
	}
	
	
}
	       
	function banner(){
	var i = 0;
	var timer;
	$(".home_slider_ul1 li").eq(0).show().siblings().hide();
	$(".home_slider_ul2 li").eq(0).css("background","#FE2966").siblings().css("background","#333");
	autoplay();
	$("#home_slider").hover(
		function(){
			$(".home_slider_pre").stop().animate({
				left:20,
				opacity:1
			},500);
			$(".home_slider_next").stop().animate({
				right:20,
				opacity:1
			},500);
			clearInterval(timer);
		},
		function(){
			$(".home_slider_pre").stop().animate({
				left:-54,
				opacity:0
			},500);
			$(".home_slider_next").stop().animate({
				right:-54,
				opacity:0
			},500);
			autoplay();
		}
	);
	$(".home_slider_pre").click(function(){
		clearInterval(timer);
		if(i==0){
			i = 4;
		}
		i--;
		show();
		autoplay();
	});
	$(".home_slider_next").click(function(){
		clearInterval(timer);
		if(i==3){
			i=-1;
		}
		i++;
		show();
		autoplay();
	});
	$(".home_slider_ul2 li").click(function(){
		i = $(this).index();
		show();
		clearInterval(timer);
	});
	function show(){
		$(".home_slider_ul1 li").eq(i).show().siblings().hide();
		$(".home_slider_ul2 li").eq(i).css("background","#FE2966").siblings().css("background","#333");
	}
	function autoplay(){
		timer = setInterval(function(){
			show();
			i++;
			if(i>3){
			i=0;
			}
		},2000);
	}
	glass()
	function glass(){
		$(".part_img").mouseover(function(){
			$(".part_img_shade").css("display","block");
			$(".part_big_img").css("display","block");
		})
		$(".part_img").mouseout(function(){
			$(".part_img_shade").hide();
			$(".part_big_img").hide();
		})
		$(".part_img").mousemove(function(e){
			var left = e.pageX - $(".part_img").offset().left - $(".part_img_shade").width()/2;
			var top = e.pageY - $(".part_img").offset().top - $(".part_img_shade").height()/2; 
			if(left<0){
				left = 0
			}else if(left >$(".part_img").width()-$(".part_img_shade").width()/2){
				left =$(".part_img").width()-$(".part_img_shade").width()/2;
			}
			if(top<0){
				top = 0
			}else if(top > $(".part_img").height()-$(".part_img_shade").width()){
				top = $(".part_img").height()-$(".part_img_shade").width()
			}
			$(".part_img_shade").css("left",left);
			$(".part_img_shade").css("top",top);
			$(".part_big_img img").css("left",-3*left);
			$(".part_big_img img").css("top",-3*top);
			$(".part_img_01").hover(function(){
				$(".part_img_a").attr("src","img/part_01.jpg");
				$(".part_img_b").attr("src","img/part_01.jpg");
			})
			$(".part_img_02").hover(function(){
				$(".part_img_a").attr("src","img/part_02.jpg");
				$(".part_img_b").attr("src","img/part_02.jpg");
			})
		})
	}
	
	//add shopcar
	
	
	
	
	
	
}
	return {
		index : Index,
		banner:banner
	}
})
